package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    RoleService roleService;
    @RequestMapping("/add")
    public Result add(Integer[] permissionIds,Integer[] menuIds,@RequestBody Role role){
        try {
            roleService.add(permissionIds,menuIds,role);
            return new Result(true,MessageConstant.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ROLE_FAIL);
        }
    }

    @RequestMapping("/queryByCondition")
    public PageResult queryByCondition(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult= roleService.queryByCondition(queryPageBean.getCurrentPage(),queryPageBean.getQueryString(),queryPageBean.getPageSize());
        return pageResult;
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role=roleService.findById(id);
            return new Result(true,MessageConstant.ADD_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ROLE_FAIL);
        }
    }

    @RequestMapping("/findPermissionByRoleId")
    public Result findPermissionByRoleId(Integer id){
        try {
            List<Integer> permissionIds = roleService.findPermissionByRoleId(id);
            return new Result(true,MessageConstant.QUERY_PERMISSIONSERVICE_SUCCESS,permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PERMISSIONSERVICE_FAIL);
        }
    }
    @RequestMapping("/findMenuByRoleId")
    public Result findMenuByRoleId(Integer id){
        try {
            List<Integer> menuIds=roleService.findMenuByRoleId(id);
            return new Result(true,MessageConstant.QUERY_MENU_SUDCCESS,menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_MENU_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(Integer[] permissionIds,Integer[] menuIds,@RequestBody Role role){
        try {
            roleService.edit(permissionIds,menuIds,role);
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
            try {
                roleService.delete(id);
                return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
            }
    }



}
